package com.vcredit.creditcat.service;

import com.vcredit.creditcat.framework.pagination.Criteria;
import com.vcredit.creditcat.framework.pagination.Page;
import com.vcredit.creditcat.model.MerchantProduct;
import com.vcredit.creditcat.model.ProductBusinessStatus;
import com.vcredit.creditcat.model.ProductCondition;
import com.vcredit.creditcat.persistence.base.MerchantProductMapper;
import com.vcredit.creditcat.persistence.base.RegisterChannelMapper;
import com.vcredit.creditcat.util.BeanUtil;
import com.vcredit.creditcat.vo.BuiSelectVo;
import com.vcredit.creditcat.vo.MerchantProductVo;
import com.vcredit.creditcat.vo.MerchantProductVoExt;
import com.vcredit.creditcat.vo.ProductNestedVo;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/*
 * @author zhangxingong
 * @version 1.0
 * @since 1.0
*/
@Service
public class MerchantProductService {

  @Autowired
  private MerchantProductMapper merchantProductMapper;
  
  @Autowired
  private ProductConditionService productConditionService;
  @Autowired
  private ProductBusinessStatusService productBusinessStatusService;

  public Page<MerchantProduct> findByPage(Criteria criteria) {
    List<MerchantProduct> merchantProductList=merchantProductMapper.findByPage(criteria);
    return new Page<MerchantProduct>(merchantProductMapper.findByPageCount(criteria),
            merchantProductList);
  }

  public List<MerchantProduct> find(MerchantProduct merchantProduct) {
    return merchantProductMapper.find(merchantProduct);
  }

  public MerchantProduct findByName(String productName) {
    return merchantProductMapper.findByName(productName);
  }

  public MerchantProduct findById(Long productId) {
    return merchantProductMapper.findById(productId);
  }

  public List<MerchantProduct> findUnTechicalProduct() {
    return find(new MerchantProduct(false));
  }

  public List<MerchantProduct> findTechicalProduct() {
    return find(new MerchantProduct(true));
  }

  public List<MerchantProduct> findAll() {
    return merchantProductMapper.findAll();
  }

  public void save(MerchantProductVo merchantProductVo) {
    try {
      MerchantProduct merchantProduct = new MerchantProduct();
      BeanUtil.copy(merchantProductVo, merchantProduct);
      merchantProductMapper.insert(merchantProduct);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void update(MerchantProduct merchantProduct) {
    merchantProductMapper.update(merchantProduct);
  }

  public void removeMerchantProduct(String ids) {
    for (String productIdString : ids.split(",")) {
      Long productId = Long.parseLong(productIdString);
      merchantProductMapper.delete(productId);
    }
  }

  @Transactional
  public void saveExt(MerchantProductVoExt merchantProductVoExt) {
    MerchantProduct merchantProduct = new MerchantProduct();
    try {
      BeanUtil.copy(merchantProductVoExt, merchantProduct);
      merchantProductMapper.insert(merchantProduct);
      Long productId=merchantProduct.getProductId();
      if(productId!=null){
        List<ProductNestedVo> conditionList = merchantProductVoExt.getConditionList();
        for (ProductNestedVo vo : conditionList) {
          ProductCondition condition = new ProductCondition();
          BeanUtil.copy(vo, condition);
          condition.setProductId(productId);
          productConditionService.insert(condition);
        }
        ProductBusinessStatus productBusinessStatus=new ProductBusinessStatus();
        productBusinessStatus.setProductId(productId);
        productBusinessStatusService.save(productBusinessStatus);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

  }

  public List<BuiSelectVo> listAllProduct() {
    List<MerchantProduct> merchantProductList = merchantProductMapper.findAll();
    List<BuiSelectVo> voList = new ArrayList<>(merchantProductList.size());
    for (MerchantProduct product :
        merchantProductList) {
      voList.add(new BuiSelectVo(product.getProductName(), String.valueOf(product.getProductId())));
    }
    return voList;
  }
  
}
